import { createRouter, createWebHashHistory } from 'vue-router';

const router = createRouter({
  history:createWebHashHistory(),
  routes:[
    {path:'/home',component:()=>import('../view/Home.vue'),
      children:[
        {path:'/homepage',component:()=>import('../view/HomePage.vue')},
        {path:'/employee',component:()=>import('../view/Employee.vue')},
        {path:'/dish',component:()=>import('../view/dish.vue')},
        {path:'/category',component:()=>import('../view/category.vue')},
        {path:'/user',component:()=>import('../view/User.vue')},
        {path:'/setmeal',component:()=>import('../view/Setmeal.vue')}
    ]
    },
    {path:'/login',name: 'EmployeeLogin',component:()=>import('../view/EmployeeLogin.vue')},
    {path:'/404',component:()=>import('../view/view404.vue')},
    {path:'/',redirect: '/login'}
  ]
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token');
  if(to.name === 'EmployeeLogin' || to.path === '/login'){
    next();
  }else{
    if(!token){
      next('/login');
    }else{
      next();
    }
  }
})

export default router